# Expect script for linker support of IFUNC symbols and relocations.
#
#   Copyright (C) 2009-2015 Free Software Foundation, Inc.
#   Contributed by Red Hat.
#
# This file is part of the GNU Binutils.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Written by Nick Clifton <nickc@redhat.com>


# IFUNC support has only been implemented for the ix86, x86_64, powerpc,
# aarch64 and sparc so far.
if {!(([istarget "i?86-*-*"]
       || [istarget "x86_64-*-*"]
       || [istarget "powerpc*-*-*"]
       || [istarget "aarch64*-*-*"]
       || [istarget "sparc*-*-*"])
      && ([istarget "*-*-elf*"]
	  || [istarget "*-*-nacl*"]
	  || (([istarget "*-*-linux*"]
	       || [istarget "*-*-gnu*"])
	      && ![istarget "*-*-*aout*"]
	      && ![istarget "*-*-*oldld*"]))) } {
    verbose "IFUNC tests not run - target does not support IFUNC"
    return
}

# We need a native system.  FIXME: Strictly speaking this
# is not true, we just need to know how to create a fully
# linked executable, including the C and Z libraries, using
# the linker that is under test.
if ![isnative] {
    verbose "IFUNC tests not run - not a native toolchain"
    return
}

# We need a working compiler.  (Strictly speaking this is
# not true, we could use target specific assembler files).
if { [which $CC] == 0 } {
    verbose "IFUNC tests not run - no compiler available"
    return
}

# A procedure to check the OS/ABI field in the ELF header of a binary file.
proc check_osabi { binary_file expected_osabi } {
    global READELF
    global READELFFLAGS

    catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got

    if ![string match "" $got] then {
	verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
	return 0
    }

    if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
	   [file_contents readelf.out] nil osabi] } {
	verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
	return 0
    }

    if { $osabi == $expected_osabi } {
	return 1
    }

    verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"

    return 0
}

# A procedure to confirm that a file contains the IFUNC symbol.
# Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
proc contains_ifunc_symbol { binary_file } {
    global READELF
    global READELFFLAGS

    catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got

    if ![string match "" $got] then {
	verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
	return -1
    }

    # Look for a line like this:
    #    58: 0000000000400600    30 IFUNC   GLOBAL DEFAULT   12 library_func2
    # with perhaps some other info between the visibility and section

    if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT .* \[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
	return 0
    }

    return 1
}

# A procedure to confirm that a file contains the R_*_IRELATIVE
# relocation.
# Returns -1 upon error, 0 if the relocation was not found and 1 if
# it was found.
proc contains_irelative_reloc { binary_file } {
    global READELF
    global READELFFLAGS

    catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got

    if ![string match "" $got] then {
	verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
	return -1
    }

    # Look for a line like this:
    #    0000000000600ab0  0000000000000025 R_X86_64_IRELATIVE      000000000040061c
    #    080496f4  0000002a R_386_IRELATIVE


    if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IREL(|ATIVE)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
	return 0
    }

    return 1
}

# A procedure to confirm that a file contains a relocation that references an IFUNC symbol.
# Returns -1 upon error, 0 if the reloc was not found and 1 if it was found.
proc contains_ifunc_reloc { binary_file } {
    global READELF
    global READELFFLAGS

    catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got

    if ![string match "" $got] then {
	verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
	return -1
    }

    if [string match "" [file_contents readelf.out]] then {
	verbose "No relocs found in $binary_file"
	return 0
    }

    if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
	return 0
    }

    return 1
}

set fails 0

# Create the object files, libraries and executables.
if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
    fail "Could not create a PIC object file"
    set fails [expr $fails + 1]
}
if ![ld_compile "$CC -c" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
    fail "Could not create a non-PIC object file"
    set fails [expr $fails + 1]
}
if ![ld_compile "$CC -c -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] {
    fail "Could not create a PIC object file containing an IFUNC symbol"
    set fails [expr $fails + 1]
}
if ![ld_compile "$CC -c -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] {
    fail "Could not create a non-PIC object file containing an IFUNC symbol"
    set fails [expr $fails + 1]
}
if ![ld_compile "$CC -c -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] {
    fail "Could not create an ordinary non-PIC object file"
    set fails [expr $fails + 1]
}
if ![ld_assemble $as "$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
    fail "Could not create an empty object file"
    set fails [expr $fails + 1]
}
if ![ld_compile "$CC -c" "$srcdir/$subdir/test-1.c" "tmpdir/test-1.o"] {
    fail "Could not create test-1.o"
    set fails [expr $fails + 1]
}
if ![ld_compile "$CC -fPIC -c" "$srcdir/$subdir/test-2.c" "tmpdir/test-2.o"] {
    fail "Could not create test-2.o"
    set fails [expr $fails + 1]
}

if { $fails != 0 } {
    return
}

if ![ld_simple_link $ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] {
    fail "Could not create a shared library containing an IFUNC symbol"
    set fails [expr $fails + 1]
}
if ![ar_simple_create $ar "" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] {
    fail "Could not create a static library containing an IFUNC symbol"
    set fails [expr $fails + 1]
}

if { $fails != 0 } {
    return
}

if ![default_ld_link $ld "tmpdir/dynamic_prog" "-Ltmpdir tmpdir/shared_prog.o -Bdynamic -lshared_ifunc -rpath ./tmpdir"] {
    fail "Could not link a dynamic executable"
    set fails [expr $fails + 1]
}
if ![default_ld_link $ld "tmpdir/local_prog" "-Ltmpdir tmpdir/static_prog.o -lifunc"] {
    fail "Could not link a dynamic executable using local ifunc"
    set fails [expr $fails + 1]
}
if ![default_ld_link $ld "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
    fail "Could not link a static executable"
    set fails [expr $fails + 1]
}
if ![ld_simple_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
    fail "Could not link a non-ifunc using static executable"
    set fails [expr $fails + 1]
}
if ![default_ld_link $ld "tmpdir/test-1" "tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
    fail "Could not link test-1"
    set fails [expr $fails + 1]
}
if ![ld_simple_link $ld "tmpdir/libtest-2.so" "-shared tmpdir/test-2.o"] {
    fail "Could not link libtest-2.so"
    set fails [expr $fails + 1]
}

if { $fails == 0 } {
  pass "Building ifunc binaries"
  set fails 0
} else {
    return
}

# Check the executables and shared libraries
#
# The linked ifunc using executables and the shared library containing
# ifunc should have an OSABI field of GNU.  The linked non-ifunc using
# executable should have an OSABI field of NONE (aka System V).

if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
    fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
    set fails [expr $fails + 1]
}
if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
    fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
    set fails [expr $fails + 1]
}
if {! [check_osabi tmpdir/static_prog {UNIX - GNU}]} {
    fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
    set fails [expr $fails + 1]
}
if {! [check_osabi tmpdir/dynamic_prog {UNIX - System V}]} {
    fail "Dynamic ifunc-using executable does not have an OS/ABI field of System V"
    set fails [expr $fails + 1]
}
if {! [check_osabi tmpdir/static_nonifunc_prog {UNIX - System V}]} {
    fail "Static non-ifunc-using executable does not have an OS/ABI field of System V"
    set fails [expr $fails + 1]
}

# The linked ifunc using executables and the shared library containing
# ifunc should contain an IFUNC symbol.  The non-ifunc using executable
# should not.

if {[contains_ifunc_symbol tmpdir/libshared_ifunc.so] != 1} {
    fail "Shared libraries containing ifunc does not contain an IFUNC symbol"
    set fails [expr $fails + 1]
}
if {[contains_ifunc_symbol tmpdir/local_prog] != 1} {
    fail "Local ifunc-using executable does not contain an IFUNC symbol"
    set fails [expr $fails + 1]
}
if {[contains_ifunc_symbol tmpdir/static_prog] != 1} {
    fail "Static ifunc-using executable does not contain an IFUNC symbol"
    set fails [expr $fails + 1]
}
if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
    fail "Dynamic ifunc-using executable contains an IFUNC symbol"
    set fails [expr $fails + 1]
}
if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} {
    fail "Static non-ifunc-using executable contains an IFUNC symbol"
    set fails [expr $fails + 1]
}
if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
    fail "test-1 contains IFUNC symbols"
    set fails [expr $fails + 1]
}
if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
    fail "libtest-2.so contains IFUNC symbols"
    set fails [expr $fails + 1]
}

# The linked ifunc using executables and shared libraries should contain
# a dynamic reloc referencing the IFUNC symbol.  (Even the static
# executable which should have a dynamic section created for it).  The
# non-ifunc using executable should not.

if {[contains_irelative_reloc tmpdir/libshared_ifunc.so] != 1} {
    fail "ifunc-using shared library does not contain R_*_IRELATIVE relocation"
    set fails [expr $fails + 1]
}
if {[contains_irelative_reloc tmpdir/local_prog] != 1} {
    fail "Local ifunc-using executable does not contain R_*_IRELATIVE relocation"
    set fails [expr $fails + 1]
}
if {[contains_irelative_reloc tmpdir/static_prog] != 1} {
    fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
    set fails [expr $fails + 1]
}
if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
    fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
    set fails [expr $fails + 1]
}
if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} {
    fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!"
    set fails [expr $fails + 1]
}

if { $fails == 0 } {
  pass "Checking ifunc binaries"
}

# Clean up, unless we are being verbose, in which case we leave the files available.
if { $verbose < 1 } {
    remote_file host delete "tmpdir/shared_prog.o"
    remote_file host delete "tmpdir/static_prog.o"
    remote_file host delete "tmpdir/shared_ifunc.o"
    remote_file host delete "tmpdir/static_ifunc.o"
    remote_file host delete "tmpdir/static_noifunc.o"
    remote_file host delete "tmpdir/libshared_ifunc.so"
    remote_file host delete "tmpdir/libifunc.a"
    remote_file host delete "tmpdir/dynamic_prog"
    remote_file host delete "tmpdir/local_prog"
    remote_file host delete "tmpdir/static_prog"
    remote_file host delete "tmpdir/static_nonifunc_prog"
}

run_cc_link_tests [list \
    [list \
	"Build libpr16467a.so" \
	"-shared -Wl,--version-script=pr16467a.map" \
	"-fPIC" \
	{ pr16467a.c } \
	{} \
	"libpr16467a.so" \
    ] \
    [list \
	"Build libpr16467b.a" \
	"" \
	"-fPIC" \
	{ pr16467b.c } \
	{} \
	"libpr16467b.a" \
    ] \
    [list \
	"Build libpr16467b.so" \
	"-shared tmpdir/pr16467b.o tmpdir/libpr16467a.so \
	 -Wl,--version-script=pr16467b.map" \
	"-fPIC" \
	{ dummy.c } \
	{} \
	"libpr16467b.so" \
    ] \
    [list \
	"Build libpr16467c.a" \
	"" \
	"" \
	{ pr16467c.c } \
	{} \
	"libpr16467c.a" \
    ] \
]

run_ld_link_exec_tests [] [list \
    [list \
	"Common symbol override ifunc test 1a" \
	"-static" \
	"" \
	{ ifunc-common-1a.c ifunc-common-1b.c } \
	"ifunc-common-1a" \
	"ifunc-common-1.out" \
	"-g" \
    ] \
    [list \
	"Common symbol override ifunc test 1b" \
	"-static" \
	"" \
	{ ifunc-common-1b.c ifunc-common-1a.c } \
	"ifunc-common-1b" \
	"ifunc-common-1.out" \
	"-g" \
    ] \
]

set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
foreach t $test_list {
    # We need to strip the ".d", but can leave the dirname.
    verbose [file rootname $t]
    run_dump_test [file rootname $t]
}

# Run-time tests which require working IFUNC support.
if { ![check_ifunc_available] } {
    return
}

run_cc_link_tests [list \
    [list \
	"Build ifunc-lib.so" \
	"-shared" \
	"-fPIC" \
	{ ifunc-lib.c } \
	{} \
	"libifunc-lib.so" \
    ] \
]

run_ld_link_exec_tests [] [list \
    [list \
	"Run pr16467" \
	"tmpdir/pr16467c.o tmpdir/libpr16467b.so tmpdir/libpr16467a.so" \
	"" \
	{ dummy.c } \
	"pr16467" \
	"pr16467.out" \
	"" \
    ] \
    [list \
	"Run ifunc-main" \
	"tmpdir/libifunc-lib.so" \
	"" \
	{ ifunc-main.c } \
	"ifunc-main" \
	"ifunc-main.out" \
    ] \
    [list \
	"Run ifunc-main with -fpic" \
	"tmpdir/libifunc-lib.so" \
	"" \
	{ ifunc-main.c } \
	"ifunc-main" \
	"ifunc-main.out" \
	"-fpic" \
    ] \
]

# Run-time tests which require working ifunc attribute support.
if { ![check_ifunc_attribute_available] } {
    return
}

run_cc_link_tests [list \
    [list \
	"Build libpr18808.so" \
	"-shared" \
	"-fPIC -O2 -g" \
	{ pr18808b.c } \
	{} \
	"libpr18808.so" \
    ] \
    [list \
	"Build libpr18841b.so" \
	"-shared" \
	"-fPIC -O0 -g" \
	{ pr18841b.c } \
	{} \
	"libpr18841b.so" \
    ] \
    [list \
	"Build libpr18841c.so" \
	"-shared" \
	"-fPIC -O0 -g" \
	{ pr18841c.c } \
	{} \
	"libpr18841c.so" \
    ] \
]

run_ld_link_exec_tests [] [list \
    [list \
	"Run pr18808" \
	"tmpdir/libpr18808.so" \
	"" \
	{ pr18808a.c } \
	"pr18808" \
	"pr18808.out" \
    ] \
    [list \
	"Run pr18841 with libpr18841b.so" \
	"tmpdir/libpr18841b.so" \
	"" \
	{ pr18841a.c } \
	"pr18841b" \
	"pr18841.out" \
    ] \
    [list \
	"Run pr18841 with libpr18841c.so" \
	"tmpdir/libpr18841c.so" \
	"" \
	{ pr18841a.c } \
	"pr18841c" \
	"pr18841.out" \
    ] \
]
